"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpExceptionFilter = void 0;
const common_1 = require("@nestjs/common");
const log4js_1 = require("../utils/log4js");
//必须实现至ExceptionFilter，固定写法，该接口只有一个catch方法
//catch方法参数：
//exception：当前正在处理的异常对象
//host：传递给原始处理程序的参数的一个包装(Response/Request)的引用
let HttpExceptionFilter = class HttpExceptionFilter {
    catch(exception, host) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const request = ctx.getRequest();
        const status = exception.getStatus();
        const exceptionRes = exception.getResponse(); //获取响应对象
        const { error, message } = exceptionRes;
        const logFormat = ` <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       Request original url: ${request.originalUrl}
       Method: ${request.method}
       IP: ${request.ip}
       Status code: ${status}
       Response: ${JSON.stringify(exception)} \n  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       `;
        log4js_1.Logger.error(logFormat);
        //自定义的异常响应内容
        const msgLog = {
            code: -1,
            timestamp: new Date().toISOString(),
            path: request.url,
            error: `${status >= 500 ? 'Service Error' : 'Client Error'}`,
            message: typeof message == 'string' ? message : JSON.stringify(message),
        };
        // response.status(status).json(msgLog); 修复网关调用时 报错 BUG
        response.status(200).json(msgLog);
    }
};
exports.HttpExceptionFilter = HttpExceptionFilter;
exports.HttpExceptionFilter = HttpExceptionFilter = __decorate([
    (0, common_1.Catch)(common_1.HttpException)
], HttpExceptionFilter);
